package com.teliasonera.crm.testsuites;

import com.teliasonera.crm.testing.CRMElements;
import com.teliasonera.crm.testing.CreateAccountPage;
import com.teliasonera.crm.testing.CustomersPage;
import com.teliasonera.crm.testing.HomePage;
import com.teliasonera.crm.testing.LoginPage;
import com.teliasonera.crm.testing.OrderListPage;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 *
 * @author eyh6112
 */
public class CreateAccounts {

    private static final String CUSTOMER_NAME = "Taide- ja teatteriyhdistys Merten Kauhu ry";
    private static final int NUMBER_OF_ACCOUNTS = 1;
    private static final String baseUrl = "http://juggernaut.stadi.sonera.fi:10000/";
    private WebDriver driver;

    public CreateAccounts() {
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }

    @Before
    public void setUp() {
        driver.get(baseUrl + "/web/");
    }

    @After
    public void tearDown() {
        driver.close();
    }

    @Test
    public void createAccounts() {

        LoginPage login = new LoginPage(driver);
        // Login
        HomePage homePage = login.attemptLogin(CRMElements.USERNAME, CRMElements.PASSWORD);
        
        for (int i = 1; i <= NUMBER_OF_ACCOUNTS; i++) {
            
            try {
                
                // Navigate to customers page
                CustomersPage customers = (CustomersPage) homePage.navigateTo(HomePage.PAGE_TITLE_CUSTOMERS);
                
                // Search for customer if customer is not already selected
                if(!customers.checkCurrentCustomer(CUSTOMER_NAME)) {
                    
                    // Define search parameters and search!
                    Map<String, String> searchParameters = new HashMap <>();
                    searchParameters.put(CustomersPage.KEY_CUSTOMER_NAME, new String(CUSTOMER_NAME));

                    customers.searchForCustomers(searchParameters);
                    customers.clickCustomerSearch(0);
                    
                }
                
                // Create new account
                CreateAccountPage createAccount = customers.createNewAccount();
                
                if(!customers.checkCurrentCustomer(CUSTOMER_NAME)) throw new Exception("Wrong customer!");
                
                Map<String, String> accountData = new HashMap <>();
                accountData.put(CreateAccountPage.ACCOUNT_NAME, new String("Test account ") + (int)(Math.random()*100000+1));
                accountData.put(CreateAccountPage.POSTAL_CODE, new String("02100"));
                accountData.put(CreateAccountPage.STREET_NAME, new String("Kaupinkalliontie"));
                accountData.put(CreateAccountPage.STREET_NUMBER, new String("4"));

                OrderListPage orderList = createAccount.createAccount(accountData);
                orderList.navigateTo(OrderListPage.PAGE_TITLE_HOME);
                
                System.out.println("::: COMPLETED " + i + " / " + NUMBER_OF_ACCOUNTS + " account(s)!");
                
            } catch(Exception e) {
                e.printStackTrace();
                driver.navigate().refresh();
            }           
        }
        
        // Sign out
        login = homePage.signOut();
        
        assertTrue(true);

    }
}